package {
  import __AS3__.vec.Vector;
  import com.bit101.components.Label;
  import com.bit101.components.PushButton;
  import flash.events.FocusEvent;
  import flash.events.MouseEvent;
  import mge.Ease;
  import mge.ui.ModalDialog;
  import mge.ui.ToolTipBox;
  
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  
  import mge.ui.Button;

  public class Buttons extends Sprite
  {
    [Embed(source="C:/lib/mge/projects/buttons/res/1.png")]
    private var r1:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/2.png")]
    private var r2:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/3.png")]
    private var r3:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/4.png")]
    private var r4:Class;
    
    [Embed(source="C:/lib/mge/projects/buttons/res/1_16.png")]
    private var r1_16:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/2_16.png")]
    private var r2_16:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/3_16.png")]
    private var r3_16:Class;
    [Embed(source="C:/lib/mge/projects/buttons/res/4_16.png")]
    private var r4_16:Class;
    
    
    private var cont:ButtonContainer;
    private const NUM_BUTTONS:int = 4;
    private var label:Label;
    private var focusMsg:Label;
    
    private var lockers:Vector.<Button> = new Vector.<Button>();
    
    public function Buttons()
    {
      // var btn:mge.ui.Button = new mge.ui.Button(new r2, new r4, new r1, new r3, this);
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      stage.stageFocusRect = false;
      
      cont = new ButtonContainer(this);
      cont.rearrangeFunction = rearrangeMargins;
      // cont.rearrangeFunction = rearr;
      
      var x:int = Math.random() * 300;
      for (var i:int = 0; i < NUM_BUTTONS; i++)
      {
        // Enabled
        var enabled:Boolean = Math.random() > 0.3;
        // Adds button
        var btn:mge.ui.Button = new mge.ui.Button("button_" + i, new r2, new r4, new r1, new r3, this);
        btn.enabled = enabled;
        btn.addOnClickCallback(OnClickButton);
        
        // Adds locker
        var locker:Button = new Button("locker_" + i, new r2_16, new r4_16, new r1_16, new r3_16, this);
        locker.addOnClickCallback(OnClickButton);
        locker.enabled = enabled;
        lockers.push(locker);
        
        cont.addButton(btn);
      }
      
      for (i = 0; i < NUM_BUTTONS; i++)
      {
          btn = cont.getButton("button_" + i);
          if (btn.enabled)
          {
              btn.selected = true;
              break;
          }
      }
      
      label = new Label(this, 150, 10);
      // btn.enable = false;
      focusMsg = new Label(this, 150, 150, "Click Here to allow flash get focus!");    
      
      
      new PushButton(this, 0, 0, "TIPS ON/OFF", onTipsChange);
      
      new PushButton(this, 0, 20, "TEST MODAL", onTestModal);
      
      stage.addEventListener(MouseEvent.CLICK, OnFocusIn);
      
      var bg:Sprite = new Sprite();
      bg.graphics.beginFill(0xCCCCCC);
      bg.graphics.drawRect(0, 0, stage.width, stage.height);
      bg.graphics.endFill();
      this.addChildAt(bg, 0);
    }
    
    private var tips:Boolean;
    private var tipsContainer:Sprite;
    private var placeHolderText:String = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.";
    private function onTipsChange(e:Event):void
    {
      tips = !tips;
      
      if (tips)
      {
        // var placeHolderText:String = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
        
        // var placeHolderText:String = "FULL SCREEN";
      
        tipsContainer = new Sprite();
        addChild(tipsContainer);
        for (var i:int = 0; i < NUM_BUTTONS; i++)
        {
          var tip:ToolTipBox = new ToolTipBox( cont.getButton("button_" + i).getSprite(), placeHolderText );
          tipsContainer.addChild( tip );
        }
      }
      else
      {
        removeChild(tipsContainer);
        tipsContainer = null;
      }
    }
    
    private function onTestModal(e:Event):void
    {
      new ModalDialog(this, "Testing Modal Dialog!", null, "Continue", onModalDialogClosed, "no", "cancel");
    }
    
    private function onModalDialogClosed(result:String):void
    {
      trace(result);
    }
    
    private function OnFocusIn(e:Event):void
    {
        focusMsg.text = "";
    }
    
    private function OnClickButton(btnName:String):void
    {
      for (var i:int = 0; i < 4; i++)
      {
        if (btnName == "locker_" + i)
        {
            var b:Button = cont.getButton("button_" + i);
            b.keyboardNavigable = !b.keyboardNavigable;
            
            var locker:Button = lockers[i];
            locker.selected = !b.keyboardNavigable;
        }
      }
      /*
      if (btnName == "button_3")
      {
        // cont.rearrangeFunction = rearrange;
        for (var i:int = 0; i < NUM_BUTTONS; i++)
        {
            var btn:Button = cont.getButton("button_" + i);
            btn.enabled = false;
        }
      }*/
      
      /*
      if (btnName == "button_2")
      {
        cont.rearrangeFunction = rearrangeSquare;
      }*/
      label.text = btnName + " pressed!";
    }
    
    private function rearrange(buttons:Vector.<Button>):void
    {        
        if (buttons.length == 0) return;
        
        var length:int = buttons.length;
        
        var sep_x:Number = 1 / (buttons.length + 1);
        var sep_y:Number = 250 / (length);
        var e:Ease = new Ease(Ease.cubicIn_01);
        
        for (var i:int = 0; i < buttons.length; i++)
        {
            var b:Button = buttons[i];                
            b.x = x + 80 * e.delta(sep_x * (buttons.length - i), 1);
            b.y = y + sep_y * i;
        }
        
    }
    
    private function rearrangeSquare(buttons:Vector.<Button>):void
    {
        if (buttons.length == 0) return;
        
        var columns:int = 10
        
        for (var i:int = 0; i < NUM_BUTTONS; i++)
        {
            buttons[i].x = (i % columns) * 80;
            var y:int = i / columns;
            buttons[i].y = y * 80;
        }
    }
    
    private function rearrangeLine(buttons:Vector.<Button>):void
    {
        if (buttons.length == 0) return;
        for (var i:int = 0; i < buttons.length; i++)
        {
            var y:Number = i / buttons.length;
            buttons[i].y = y * 270;
            lockers[i].x = buttons[i].x + 64;
            lockers[i].y = buttons[i].y + 50;
        }
    }
    
    private function rearrangeMargins(buttons:Vector.<Button>):void
    {
      if (buttons.length == 0) return;
      
      for (var i:int = 0; i < buttons.length; i++)
      {
        var b:Button = buttons[i];
        var f:Number = (i / buttons.length) * (Math.PI * 2)
        var rad:Number = 200;
        b.x = rad + Math.sin(f) * rad;
        b.y = rad + Math.cos(f) * rad;
        //b.x = Math.random() * 400;
        //b.y = Math.random() * 400;
      }
    }
    
  }
}
